package com.example.back.mapper;

import com.example.back.entity.Post;
import com.example.back.entity.otd.Posts;
import org.apache.ibatis.annotations.*;

import java.util.ArrayList;
import java.util.List;

@Mapper
public interface PostMapper {

    @Insert("insert into post(userId,time,title,text,browse,url) values(#{userId},#{time},#{title},#{text},#{browse},#{url})")
    int addPost(Post post);

    @Delete("delete from post where id=#{param1}")
    int deletePost(Integer id);
    @UpdateProvider(type =PostUpdateProvider.class, method = "getUpdatePostSql")
    int updatePost(Post post);
    @Update("UPDATE post SET browse = COALESCE(browse, 0) + 1 WHERE id = #{id}")
    int addBrowse(Integer id);

    @Update("UPDATE post SET dayBrowse = COALESCE(dayBrowse, 0) + 1 WHERE id = #{id}")
    int addDayBrowse(Integer id);

    @Update("UPDATE post SET dayBrowse = 0")
    int resetDayBrowse();
    @Select("select * from post where id=#{param1}")
    Post queryPostById(Integer id);
    @SelectProvider(type =PostUpdateProvider.class, method = "getQueryPostSql")
    ArrayList<Post> queryPost(Post post);
    @Select("SELECT * FROM post ORDER BY dayBrowse DESC LIMIT 20")
    ArrayList<Post> getTopPostsByBrowse();

    // 查询用户关注的人的帖子
    @Select("""
        SELECT p.* 
        FROM post p
        JOIN relations r ON p.userId = r.userId 
        WHERE r.fanId = #{fanId}
        ORDER BY p.time DESC
    """)
    List<Post> selectPostsByFollowedUsers(@Param("fanId") Integer fanId);
    @Select("select * from post where userId=#{param1}")
    ArrayList<Post> queryPostByUserId(Integer userId);


    @Select("SELECT time FROM post WHERE DATE(time) = CURDATE()")  // 直接查询所有帖子的发布时间字段
    List<String> getAllPostTimes();
}
